home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / arm / gui / configPanel.py < prev    next >
Encoding:
Python Source  |  2012-05-18  |  5.4 KB  |  162 lines

  1. """
  2. Configuration panel.
  3. """
  4.  
  5. import random
  6. import sys
  7. import time
  8.  
  9. import gobject
  10. import gtk
  11.  
  12. from cli.configPanel import (ConfigPanel as CliConfigPanel, Field, State)
  13. from util import connections, gtkTools, sysTools, torConfig, torTools, uiTools
  14. from TorCtl import TorCtl
  15.  
  16. CATEGORY_COLOR = {torConfig.Category.GENERAL: "#307809",
  17.                   torConfig.Category.CLIENT: "#2F305C",
  18.                   torConfig.Category.RELAY: "#848144",
  19.                   torConfig.Category.DIRECTORY: "#9F2254",
  20.                   torConfig.Category.AUTHORITY: "#B3141B",
  21.                   torConfig.Category.HIDDEN_SERVICE: "#3A8427",
  22.                   torConfig.Category.TESTING: "#222222",
  23.                   torConfig.Category.UNKNOWN: "#111111"}
  24.  
  25. def input_conf_value_size(option, oldValue):
  26.   prompt = "Enter value for %s" % option
  27.   return gtkTools.input_size(prompt, oldValue)
  28.  
  29. def input_conf_value_time(option, oldValue):
  30.   prompt = "Enter value for %s" % option
  31.   return gtkTools.input_time(prompt, oldValue)
  32.  
  33. def input_conf_value_int(option, oldValue):
  34.   prompt = "Enter value for %s" % option
  35.   return gtkTools.input_int(prompt, oldValue)
  36.  
  37. def input_conf_value_list(option, oldValue, csv=False):
  38.   prompt = "Enter value for %s" % option
  39.   return gtkTools.input_list(prompt, oldValue, csv)
  40.  
  41. def input_conf_value_string(option, oldValue):
  42.   prompt = "Enter value for %s" % option
  43.   return gtkTools.input_string(prompt, oldValue)
  44.  
  45. def input_conf_value_bool(option, oldValue):
  46.   prompt = "Select value for %s" % option
  47.  
  48.   newValue = gtkTools.input_bool(prompt, oldValue)
  49.  
  50.   if newValue == None:
  51.     return
  52.  
  53.   return "1" if newValue else "0"
  54.  
  55. def input_conf_value_dir(option, oldValue):
  56.   prompt = "Select value for %s" % option
  57.   return gtkTools.input_dir(prompt, oldValue)
  58.  
  59. def input_conf_value_filename(option, oldValue):
  60.   prompt = "Select value for %s" % option
  61.   return gtkTools.input_filename(prompt, oldValue)
  62.  
  63. class ConfContents(gtkTools.ListWrapper):
  64.   def _create_row_from_value(self, entry):
  65.     option = entry.get(Field.OPTION)
  66.     isDefault = entry.get(Field.IS_DEFAULT)
  67.     value = entry.get(Field.VALUE)
  68.     configType = entry.get(Field.TYPE)
  69.     summary = entry.get(Field.SUMMARY)
  70.     desc = " ".join(entry.get(Field.DESCRIPTION).split())
  71.     argUsage = entry.get(Field.ARG_USAGE)
  72.     category = entry.get(Field.CATEGORY)
  73.  
  74.     descText = "%s (%s Option)" % (option, category)
  75.     descText += "\nValue: %s (%s%s, usage: %s)" % (value, "default, " if isDefault else "", configType, argUsage)
  76.     descText += "\nDescription: %s" % (desc)
  77.  
  78.     row = (option, value, summary, CATEGORY_COLOR[category], descText)
  79.  
  80.     return row
  81.  
  82. class ConfigPanel(object, CliConfigPanel):
  83.   def __init__(self, builder):
  84.     CliConfigPanel.__init__(self, None, State.TOR)
  85.  
  86.     self.builder = builder
  87.  
  88.     listStore = self.builder.get_object('liststore_config')
  89.     self._wrappedConfContents = ConfContents(self.confContents, listStore)
  90.  
  91.   @property
  92.   def confContents(self):
  93.     if hasattr(self, '_wrappedConfContents'):
  94.       return self._wrappedConfContents.container
  95.     else:
  96.       return []
  97.  
  98.   @confContents.setter
  99.   def confContents(self, value):
  100.     if hasattr(self, '_wrappedConfContents'):
  101.       self._wrappedConfContents.empty()
  102.       for entry in value:
  103.         self._wrappedConfContents.append(entry)
  104.     else:
  105.       self._wrappedConfContents = ConfContents(value)
  106.  
  107.   def pack_widgets(self):
  108.     treeView = self.builder.get_object('treeview_config')
  109.  
  110.     treeView.connect('cursor-changed', self.on_treeview_config_cursor_changed)
  111.     treeView.connect('row-activated', self.on_treeview_config_row_activated)
  112.  
  113.   def on_treeview_config_cursor_changed(self, treeView, data=None):
  114.     treeSelection = treeView.get_selection()
  115.  
  116.     (model, iter) = treeSelection.get_selected()
  117.     desc = model.get_value(iter, 4)
  118.  
  119.     textBuffer = self.builder.get_object('textbuffer_config_desc')
  120.     textBuffer.set_text(desc)
  121.  
  122.   def on_treeview_config_row_activated(self, treeView, path, column):
  123.     (index,) = path
  124.  
  125.     entry = self._wrappedConfContents[index]
  126.     configOption = entry.get(Field.OPTION)
  127.     configType = entry.get(Field.TYPE)
  128.     argUsage = entry.get(Field.ARG_USAGE)
  129.     oldValue = entry.get(Field.VALUE) if entry.get(Field.VALUE) != '<none>' else None
  130.     newValue = None
  131.  
  132.     if configType == 'DataSize':
  133.       newValue = input_conf_value_size(configOption, oldValue)
  134.     elif configType == 'TimeInterval':
  135.       newValue = input_conf_value_time(configOption, oldValue)
  136.     elif configType == 'Integer':
  137.       newValue = input_conf_value_int(configOption, oldValue)
  138.     elif configType == 'String':
  139.       newValue = input_conf_value_string(configOption, oldValue)
  140.     elif configType == 'LineList':
  141.       newValue = input_conf_value_list(configOption, oldValue, csv=False)
  142.     elif configType == 'RouterList' or configType == 'CommaList':
  143.       newValue = input_conf_value_list(configOption, oldValue, csv=True)
  144.     elif configType == 'Boolean':
  145.       newValue = input_conf_value_bool(configOption, oldValue)
  146.     elif configType == 'Filename':
  147.       if 'DIR' in argUsage:
  148.         newValue = input_conf_value_dir(configOption, oldValue)
  149.       else:
  150.         newValue = input_conf_value_filename(configOption, oldValue)
  151.     else:
  152.       newValue = input_conf_value_string(configOption, oldValue)
  153.  
  154.     if newValue and newValue != oldValue:
  155.       try:
  156.         torTools.getConn().setOption(configOption, newValue)
  157.       except TorCtl.ErrorReply, err:
  158.         gtkTools.showError(str(err))
  159.  
  160.     self._wrappedConfContents[index] = entry
  161.  
  162.